package com.mew.chess.core.game.lh.game;

import cn.hutool.core.util.StrUtil;
import com.mew.chess.core.common.handler.ChessStart;
import com.mew.chess.core.common.handler.ChessStartAnnotation;
import com.mew.chess.core.common.utils.ChessGameUtil;
import com.mew.chess.core.game.lh.game.common.LhConstantValue;
import com.mew.chess.core.game.lh.game.common.LhLinkType;
import com.mew.chess.core.game.lh.game.message.LhHistoryMessage;
import com.mew.chess.core.game.lh.game.message.LhUserMessage;
import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.common.dc.redis.GameRoomRedisService;
import com.mew.common.entity.config.Room;
import com.zebra.common.concurrent.ZebraExecutorManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@ChessStartAnnotation("LH")
@Service
@Slf4j
public class LhStartService implements ChessStart {
    @Autowired
    private GameRoomRedisService gameRoomRedisService;

    @Override
    public void initializeGame(int gameId) throws Exception {
        LhGameManager.getInstance().setGameId(gameId);
        addRoomManager(gameId);
        log.info("-------LH---gameId:{}---start-success-----", gameId);
    }

    private void addRoomManager(int gameId)throws Exception{
        List<Room> roomList = gameRoomRedisService.findRoomByGameId(gameId);
        if (null == roomList || roomList.isEmpty()){
            throw new Exception("Start Game "+ gameId +" room is null");
        }

        for (Room room : roomList){
            initializeRoomManager(room);
        }
    }

    private void initializeRoomManager(Room room){
        LhRoomManager roomController = new LhRoomManager(room.getGameId(), room.getId());
        roomController.setBetStrs(room.getBetArrs());
        roomController.setBetArrays(StrUtil.splitToLong(room.getBetArrs(), ","));
        roomController.setBetMax(roomController.getBetArrays()[0]*LhConstantValue.MAX_BET_LIMIT);
        roomController.setMinGoldNumber(room.getMinGoldNumber());
        roomController.setOverTime(LhConstantValue.OVER_TIME);

        roomController.setLinkType(LhLinkType.Prepare.code());
        roomController.setRuleTime(LhConstantValue.PREPARE_TIME);
        roomController.setHistoryVOList(new LhHistoryMessage());
        roomController.setBigRList(initializeBigPlayer(room.getMinGoldNumber()));
        roomController.addOnlineALLByList(initializeOnlinePlayer(room.getMinGoldNumber()));
        roomController.setLhScheduleManager(new LhScheduleManager(room.getId(), ZebraExecutorManager.getInstance().getDefaultScheduler(), ZebraExecutorManager.getInstance().getDefaultExecutor()));
        roomController.setIno(ChessGameUtil.generateINO(0));
        LhGameManager.getInstance().putController(roomController);
    }

    private List<LhUserMessage> initializeBigPlayer(int betNumber){
        List<LhUserMessage> list = new ArrayList<>(LhConstantValue.BIG_NUMBER);
        if (LhConstantValue.ROBOT_ONLINE_ROOM>0){
            for (int i = 0; i< LhConstantValue.BIG_NUMBER; i++){
                LhUserMessage userMessage = new LhUserMessage();
                userMessage.buildRobotUserMessage(betNumber, LhConstantValue.BIG_GOLD);
                list.add(userMessage );
            }
        }

        return list;
    }

    private List<LhUserMessage> initializeOnlinePlayer(int betNumber){

        List<LhUserMessage> list = new ArrayList<>(LhConstantValue.ROBOT_ONLINE_ROOM);
        for (int i = 0; i< LhConstantValue.ROBOT_ONLINE_ROOM; i++){
            LhUserMessage userMessage = new LhUserMessage();
            userMessage.buildRobotUserMessage(betNumber, LhConstantValue.BIG_GOLD);
            list.add(userMessage );
        }
        return list;
    }

    public void initializeRoomManager(int gameId, int roomId){
        Room room = gameRoomRedisService.getRedisRoom(gameId, roomId);
        if (null == room){
            return;
        }
        initializeRoomManager(room);
    }
}
